package com.wei.groupingcomparator;

import java.io.IOException;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
import org.apache.hadoop.mapreduce.Reducer;


public class OrderTest {
	
	//Order_0000001,Pdt_01	222.8
	static class OrderMap extends Mapper<LongWritable, Text, OrderInfo, NullWritable>{
		OrderInfo orderInfo=new OrderInfo();
		@Override
		protected void map(LongWritable key, Text line, Context context)
				throws IOException, InterruptedException {
			String[] fields = line.toString().split(",");
			orderInfo.set(fields[0], fields[1], Double.valueOf(fields[2]));
			context.write(orderInfo, NullWritable.get());
		}
		
	}
	
	static class OrderReduce extends Reducer<OrderInfo, NullWritable, OrderInfo, NullWritable>{

		@Override
		protected void reduce(OrderInfo orderInfo, Iterable<NullWritable> iterable,Context context)
				throws IOException, InterruptedException {
			context.write(orderInfo, NullWritable.get());
		}
		
	}
	
	public static void main(String[] args) throws Exception {
		Configuration configuration = new Configuration();
		Job job = Job.getInstance(configuration);
		job.setJarByClass(OrderTest.class);
		job.setMapperClass(OrderMap.class);
		job.setReducerClass(OrderReduce.class);

		job.setMapOutputKeyClass(OrderInfo.class);
		job.setMapOutputValueClass(NullWritable.class);
		job.setGroupingComparatorClass(OrderGroupingComparator.class);
		job.setOutputKeyClass(OrderInfo.class);
		job.setOutputValueClass(NullWritable.class);
		FileInputFormat.setInputPaths(job, new Path(args[0]));
		FileOutputFormat.setOutputPath(job, new Path(args[1]));
		boolean waitForCompletion = job.waitForCompletion(true);
		System.exit(waitForCompletion ? 0 : 1);
	}

}
